package com.demo.common.util;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;

public class FileKit {
    private static Logger logger = LoggerFactory.getLogger(FileKit.class);
    public static String sp = System.getProperty("file.separator");

    /**
     * 获取文件Md5
     */
    public static String getFileMd5(MultipartFile file) throws IOException {
        if (file == null) {
            return "";
        }
        return getFileMd5(file.getInputStream());
    }

    /**
     * 获取文件MD5大写
     */
    public static String getFileMd5(InputStream fis) {
        String Md5;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] buffer = new byte[10 * 1024 * 1024];
            int length = -1;
            while ((length = fis.read(buffer, 0, buffer.length)) != -1) {
                md.update(buffer, 0, length);
            }
            BigInteger bigInt = new BigInteger(1, md.digest());
            Md5 = bigInt.toString(16);
            int a = (32 - Md5.length());
            StringBuilder aa = new StringBuilder();
            for (int i = 0; i < a; i++) {
                aa.append("0");
            }
            Md5 = (aa + Md5);
        } catch (Exception e) {
            return null;
        }
        return Md5.toUpperCase();
    }

    /**
     * 下载文件
     *
     * @param inputStream 文件流
     * @param fileName    文件名
     * @param response    返回对象
     */
    public static void downloadFile(InputStream inputStream, String fileName, HttpServletResponse response) throws IOException {
        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            byte[] buffer = new byte[1024 * 1024 * 2];
            int readLength = inputStream.read(buffer);

            while (readLength != -1) {
                outputStream.write(buffer, 0, readLength);
                outputStream.flush();
                readLength = inputStream.read(buffer);
            }
            inputStream.close();
            outputStream.close();

        } catch (IOException e) {
            logger.error(ExceptionUtils.getStackTrace(e));
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }

    }


    public static void delete(File file) {
        if (file != null && file.exists()) {
            if (file.isFile()) {
                file.delete();
            } else if (file.isDirectory()) {
                File[] files = file.listFiles();
                if (files != null) {
                    for (int i = 0; i < files.length; ++i) {
                        delete(files[i]);
                    }
                }
                file.delete();
            }
        }

    }

    /**
     * 获取文件后缀
     */
    public static String getSuffix(String name) {
        if (name.lastIndexOf(".") == -1) {
            return "";
        }
        return name.substring(name.lastIndexOf(".") + 1);
    }

}



